<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox v-for="ele in list" :key="ele.id" v-model="roleIds" :label="ele.id">
      {{ ele.name }}
    </el-checkbox>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailAPI, assignRolesAPI } from '@/api/employee'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.initRoles()
  },
  methods: {
    hSubmit() {
      this.assignRoles()
    },
    async assignRoles() {
      try {
        await assignRolesAPI({ id: this.curId, roleIds: this.roleIds })
        this.$emit('success')
      } catch (err) {
        console.log('', err)
      }
    },
    closeDialog() {},
    // 获取角色列表
    async initRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        // 保存数据
        this.list = res.data.rows
      } catch (err) {
        console.log('', err)
      }
    },
    // 获取详情
    async getUserDetail() {
      try {
        const res = await getUserDetailAPI(this.curId)
        console.log('', res)
        // 保存数据
        console.log(res.data.roleIds)
        this.roleIds = res.data.roleIds
      } catch (err) {
        console.log('', err)
      }
    }
  }
}
</script>
